import Vue from 'vue'
import App from './App.vue'
import Axios from 'axios'
import VueAxios from 'vue-axios'
import ViewUI, {Notice} from 'view-design';
import VueRouter from "vue-router";
import 'view-design/dist/styles/iview.css';
import routers from "@/routers";
import axios from "axios";
import globalData from "@/utils/globalData";

Vue.use(ViewUI);
Vue.use(VueRouter)
Vue.use(VueAxios, Axios)
Vue.config.productionTip = false
Vue.prototype.$Notice = Notice
axios.defaults.baseURL = 'http://39.106.176.101:8080/'
axios.defaults.withCredentials = true
Vue.prototype.$axios = axios

const routes = routers.routes
const router = new VueRouter({
    routes
})
document.title="海淘"
// router.beforeEach(((to, from, next) => {
//     if(to.meta.title){
//         document.title=to.meta.title
//     }
//     next()
// }))
new Vue({
    render: h => h(App),
    router
}).$mount('#app')

axios.interceptors.request.use(config => {
    if (globalData.TOKEN) {
        config.headers.common['token'] = globalData.TOKEN
    }
    return config;
}, error => {
    // 对请求错误做些什么
    return Promise.reject(error);
})


axios.interceptors.response.use(
    response => {

        return response;
    },
    error => {
        if (error.response) {
            switch (error.response.status) {
                case 401:
                    globalData.delToken();
                    router.replace({
                        path: '/SignIn',
                        // query: {redirect: router.currentRoute.fullPath}//登录成功后跳入浏览的当前页面
                    })
            }
        }
        return Promise.reject(error.response.data)
    });